﻿'/*
' * FAHWatch7 Copyright Marvin Westmaas ( mtm )
' *
' * This program is free software; you can redistribute it and/or
' * modify it under the terms of the GNU General Public License
' * as published by the Free Software Foundation; version 2
' * of the License. See the included file GPLv2.TXT.
' * 
' * This program is distributed in the hope that it will be useful,
' * but WITHOUT ANY WARRANTY; without even the implied warranty of
' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' * GNU General Public License for more details.
' * 
' * You should have received a copy of the GNU General Public License
' * along with this program; if not, write to the Free Software
' * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
' */
'/*
Public Class clsBussy
    Implements IDisposable

    Private _frm As frmBussy
    Private Structure sStoredState
        Property Enabled As Boolean
        Property WindowState As FormWindowState
        Property ShowInTaskBar As Boolean
    End Structure
    Private dStoredState As New Dictionary(Of Form, sStoredState)
    Public ReadOnly Property IsFormVisible As Boolean
        Get
            If IsNothing(_frm) Then Return False
            Return _frm.Visible
        End Get
    End Property
    Public Sub CloseForm()
        Try
            If Not IsNothing(_frm) Then
                _frm.CloseForm()
                _frm = Nothing
            End If
            If dStoredState.Count > 0 Then
                For Each Frm As Form In Application.OpenForms
                    If dStoredState.ContainsKey(Frm) Then
                        SetWindowState(Frm, dStoredState(Frm).WindowState)
                        SetEnabled(Frm, dStoredState(Frm).Enabled)
                        SetShowInTaskbar(Frm, dStoredState(Frm).ShowInTaskBar)
                    End If
                Next
                dStoredState.Clear()
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Public Delegate Sub dWindowState(frm As Form, fState As FormWindowState)
    Public Sub SetWindowState(frm As Form, State As FormWindowState)
        If frm.InvokeRequired Then
            Dim nI As New dWindowState(AddressOf SetWindowState)
            frm.Invoke(nI, {[frm], [State]})
        Else
            frm.WindowState = State
        End If
    End Sub
    Public Delegate Sub dSetEnabled(frm As Form, Enabled As Boolean)
    Public Sub SetEnabled(frm As Form, Enabled As Boolean)
        If frm.InvokeRequired Then
            Dim nI As New dSetEnabled(AddressOf SetEnabled)
            frm.Invoke(nI, {[frm], [Enabled]})
        Else
            frm.Enabled = Enabled
        End If
    End Sub
    Public Delegate Sub dShowInTaskbar(frm As Form, Show As Boolean)
    Public Sub SetShowInTaskbar(frm As Form, Show As Boolean)
        If frm.InvokeRequired Then
            Dim nI As New dShowInTaskbar(AddressOf SetShowInTaskbar)
            frm.Invoke(nI, {[frm], [Show]})
        Else
            frm.ShowInTaskbar = Show
        End If
    End Sub
    Public Sub SetMessage(Message As String)
        If Not IsNothing(_frm) Then _frm.setMessage(Message)
    End Sub
    Public Sub ShowForm(Optional Message As String = "Bussy... please wait", Optional formSize As Size = Nothing, Optional formLocation As Point = Nothing)
        Try
            If IsNothing(_frm) Then
                dStoredState.Clear()
                For Each frm As Form In Application.OpenForms
                    If Not ReferenceEquals(frm, frmLogWindow) Then
                        Dim nState As New sStoredState
                        nState.Enabled = frm.Enabled
                        nState.WindowState = frm.WindowState
                        nState.ShowInTaskBar = frm.ShowInTaskbar
                        dStoredState.Add(frm, nState)
                        SetEnabled(frm, False)
                    End If
                Next
                _frm = New frmBussy(FormBorderStyle.FixedDialog)
                _frm.ControlBox = True
                _frm.MinimizeBox = True
                _frm.MaximizeBox = False
                _frm.Show()
            End If
            _frm.setMessage(Message.ToString)
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
                Try
                    If Not IsNothing(_frm) Then
                        _frm.Dispose()
                    End If
                Catch ex As Exception : End Try
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
